﻿using System;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

namespace Blackbird.Core.Entities.Sprites
{
    public class TextureEntry : IComparable<TextureEntry>
    {
        private readonly string _hash;

        private readonly Texture2D _texture;

        public TextureEntry(byte[] imageData, GraphicsDevice device, int width, int height)
        {
            MD5 hashProvider = MD5.Create();
            byte[] imageDataHash = hashProvider.ComputeHash(imageData);
            if (device != null)
            {
                _texture = new Texture2D(device, width, height);
                _texture.SetData(imageData);
            }
            var sb = new StringBuilder();
            foreach (byte b in imageDataHash)
                sb.Append(b.ToString("x2"));

            _hash = sb.ToString();
        }

        public string Hash
        {
            get { return _hash; }
        }

        public Texture2D Texture
        {
            get { return _texture; }
        }

        #region IComparable<TextureEntry> Members

        public int CompareTo(TextureEntry other)
        {
            return Hash.CompareTo(other);
        }

        #endregion
    }
}